// components/akEditor/akEditor.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    placeholder: {
      type: String,
      value: '请输入'
    },
    background: {
      type: String,
      value: "#F5F6FA"
    },
    html: {
      type: String,
      observer: function (value) {
        if (this.data.isFocus) return
        if (this.editorCtx) {
          this.editorCtx.setContents({
            html: value
          })
        } else {
          setTimeout(() => {
            this.editorCtx.setContents({
              html: value
            })
          }, 1000);
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShowTool: false,
    isFocus: false,
    formats: {},
    readOnly: false,
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false
  },
  ready() {
    const platform = wx.getSystemInfoSync().platform
    const isIOS = platform === 'ios'
    this.setData({
      isIOS
    })
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      console.log("展开-组件", res)
      if (res.height === keyboardHeight) return
      keyboardHeight = res.height

      that.updatePosition(keyboardHeight)
      that.editorCtx.scrollIntoView()
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    bindinput(e) {
      this.triggerEvent("input", {
        value: e.detail.html
      })
    },
    readOnlyChange() {
      this.setData({
        readOnly: !this.data.readOnly
      })
    },
    // onLoad() {
    //   const platform = wx.getSystemInfoSync().platform
    //   const isIOS = platform === 'ios'
    //   this.setData({ isIOS})
    // },
    updatePosition(keyboardHeight) {
      // const toolbarHeight = 50
      // const {
      //   windowHeight,
      //   platform
      // } = wx.getSystemInfoSync()
      // let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
      this.setData({
        // editorHeight,
        keyboardHeight
      })
    },
    calNavigationBarAndStatusBar() {
      const systemInfo = wx.getSystemInfoSync()
      const {
        statusBarHeight,
        platform
      } = systemInfo
      const isIOS = platform === 'ios'
      const navigationBarHeight = isIOS ? 44 : 48
      return statusBarHeight + navigationBarHeight
    },
    onEditorReady() {
      const that = this
      this.createSelectorQuery().select('#editor').context(function (res) {
        that.editorCtx = res.context
      }).exec()
    },
    blur() {
      this.editorCtx.blur()
    },
    format(e) {
      let {
        name,
        value
      } = e.target.dataset
      if (!name) return
      // console.log('format', name, value)
      this.editorCtx.format(name, value)

    },
    bindfocus(e) {
      this.setData({
        isFocus: true
      })
    },
    bindblur(e) {
      this.setData({
        isFocus: false
      })
    },
    onStatusChange(e) {
      const formats = e.detail
      this.setData({
        formats
      })
    },
    insertDivider() {
      this.editorCtx.insertDivider({
        success: function () {
          console.log('insert divider success')
        }
      })
    },
    clear() {
      this.editorCtx.clear({
        success: function (res) {
          console.log("clear success")
        }
      })
    },
    removeFormat() {
      this.editorCtx.removeFormat()
    },
    insertDate() {
      const date = new Date()
      const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
      this.editorCtx.insertText({
        text: formatDate
      })
    },
    insertImage() {
      const that = this
      wx.chooseImage({
        count: 1,
        success: function (res) {
          that.editorCtx.insertImage({
            src: res.tempFilePaths[0],
            data: {
              id: 'abcd',
              role: 'god'
            },
            width: '80%',
            success: function () {
              console.log('insert image success')
            }
          })
        }
      })
    }
  }
})